<?
require_once("inc_security.php"); 
$record_id = getValue('record_id');
if($admin_role['rol_vip'] != 1){
    exit();
}
$stat = getValue('stat');
$db_data = new db_query('SELECT * FROM authors WHERE aut_id = '.$record_id);
if($row 		= mysql_fetch_assoc($db_data->result)){
	foreach($row as $key=>$value){
		if($key!='lang_id' && $key!='admin_id') $$key = $value;
	}
}else{
		exit();
}
if($stat == 1){
    $menu			= new menu();
	$listAll        = $menu->getAllChild("categories_multi","cat_id","cat_parent_id","0","cat_active = 1 ","cat_id,cat_name,cat_type","cat_type DESC,cat_order ASC, cat_name ASC","cat_has_child");
	$new_cat_id	=	array();
	foreach($listAll as $i=>$cat){
        $tt = '';
        for($j=0;$j<$cat["level"];$j++) $tt .= '|--';
        $new_cat_id[$cat["cat_id"]] = $tt . $cat["cat_name"];
	}
    //Thống kê news
    
    $total_row					     =   0;
	$sql							 =	 "";
    //Lấy bộ lọc filter
    $action = getValue('action','str','POST');
    if($action == 'filter'){
        $filterdate = getValue('filterdate','str','POST','');
        $filtermonth = getValue('filtermonth','int','POST');
        $filteryear = getValue('filteryear','int','POST');
        if($filtermonth){
            //Nếu tháng tồn tại thì ưu tiên lọc theo tháng
            $year = getdate();
            $year = $filteryear ? $filteryear : $year['year'];
            $startdate = @mktime(0,0,0,$filtermonth,1,$year);
            $enddate = @mktime(23,59,59,$filtermonth,cal_days_in_month(CAL_GREGORIAN,$filtermonth,$year),$year);
        }
        if($filterdate && !$filtermonth){
            //Nếu không chọn tháng thì lọc theo ngày
            $startdate = convertDateTime($filterdate,'0-0-0');
            $enddate = convertDateTime($filterdate,'23-59-59');
        }
        $sql .= ' AND new_date >= '.$startdate.' AND new_date <= '.$enddate;
    }
    $start_record                    =   getValue('start_record','int','GET',0);
    $total						= new db_count("SELECT COUNT(*) AS count
											     FROM news  
                                                 LEFT JOIN categories_multi ON new_cat_id = cat_id 
                                                 LEFT JOIN authors ON aut_id = new_author 
			                                     WHERE 1 AND new_author = ".$record_id."
                                                        AND new_status = 6 
                                                        AND new_date <= ".time().$sql);

	$db_listing					= new db_query("SELECT *
									    		FROM news
                                                LEFT JOIN categories_multi ON new_cat_id = cat_id 
                                                LEFT JOIN authors ON aut_id = new_author 
										 		WHERE 1 AND new_author = ".$record_id."
                                                    AND new_status = 6 
                                                    AND new_date <= ".time(). $sql. "
												ORDER BY  new_date DESC, new_id DESC
                                                LIMIT ".$start_record.",120  
					 							");   
    $total_row					=	mysql_num_rows($db_listing->result);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?=$load_header?>
<script language="javascript" src="../../resource/js/swfObject.js"></script>
<style>
.hidden{display:none}
</style>
</head>
<body topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<?/*---------Body------------*/?>
<?=template_top(translate_text("Management Authors listing"))?>
	<? /*---------Body------------*/ ?>
    <div class="header_listing t5" style="position: fixed;padding:5px;margin:0;border:1px solid #aaa">
        <form method="post" action="<?=getURL(0,1)?>">
            <span>Thống kê tác giả : <strong><?=$row['aut_name']?></strong></span>
            &nbsp;|&nbsp;
            <span>Tổng số bài viết : <strong><?=$total->total?></strong></span>
            &nbsp;|&nbsp;
            <span>Lọc theo ngày : <input type="text" class="textbox" name="filterdate" id="filterdate" style="width:100px;font-size:11px;" onkeypress="displayDatePicker('filterdate', this);" onclick="displayDatePicker('filterdate', this);" onfocus="if(this.value=='Enter date') this.value=''" onblur="if(this.value=='') this.value='Enter date'" value="<?=getValue('filterdate','str','POST') ? getValue('filterdate','str','POST') : date('d/m/Y',time())?>"/></span>
            &nbsp;|&nbsp;
            <span>Lọc theo tháng/năm : Tháng <input type="text" value="<?=getValue('filtermonth','int','POST','')?>"  name="filtermonth" style="width: 30px;"/> Năm <input type="text" value="<?=getValue('filteryear','int','POST','')?>"  name="filteryear" style="width: 40px;"/></span>
            &nbsp;&nbsp;
            <input type="submit" value="Lọc kết quả" class="bottom"/>
            <input type="hidden" value="filter" name="action"/>
        </form>
    </div>
    
	<table width="100%" border="1" cellpadding="3" cellspacing="0" class="table" bordercolor="<?=$fs_border?>" style="margin-top:40px;">
		<tr height="25">
			<td width="20" class="bold bg">No</td>
			<td align="center" class="bold bg" nowrap="nowrap"><?=translate_text("Tiêu đề tin")?></td>
            <td align="center" class="bold bg" nowrap="nowrap"><?=translate_text("Mô tả")?></td>
			<td align="center" class="bold bg"><?=translate_text("Loại bài")?></td>
            <td align="center" class="bold bg"><?=translate_text("Quy mô bài viết")?></td>
            <td align="center" class="bold bg"><?=translate_text("Chấm điểm")?></td>
            <td align="center" class="bold bg"><?=translate_text("Thành tiền")?></td>
        </tr>
    <?
	$i=0; 
	while($row  = mysql_fetch_assoc($db_listing->result)){
		$i++;
		?>
  		<tr>
            <td align="center"><?=$i?></td>
      		<td align="center" style="width:400px;overflow: hidden;">
    			<div style="float:left;width: 100px; overflow: hidden; border: solid 1px #F2F2F2; margin-bottom: 3px;padding:10px 0px" align="center">
    				<a href="<?=generate_detail_url($row)?>" target="_blank"><img width="100px;" src="../../../pictures/news/small_<?=$row["new_picture"]?>" onerror="errorimage(this);" /></a>
    			</div>
                <div class="news_detail" style="float:left;width:280px;padding:5px;">
                    <a href="<?=generate_detail_url($row)?>" target="_blank"><h3 style="text-align: left;float:left;"><?=$row['new_title']?></h3></a>
                    <p style="float:left;text-align: left;"><?=$row['new_teaser']?></p>
                </div>
     		</td>
            <td align="center" style="width: 250px;">
                <table>
                    <tr>
                        <td class="form_name">Danh mục : </td>
                        <td class="form_text"><?=$row['cat_name']?></td>
                    </tr>
                    <tr>
                        <td class="form_name">Ngày đăng : </td>
                        <td class="form_text"><?=date('d/m/y -  H:i',$row['new_date'])?></td>
                    </tr>
                    <tr>
                        <td class="form_name">Số từ : </td>
                        <td class="form_text"><?=count_word($row['new_detail']);?></td>
                    </tr>
                </table>
            <td>
                <select class="news_kind form_control">
                    <option value="0">--Chọn--</option>
                    <option value="1">Tin nước ngoài</option>
                    <option value="2">Tin trong nước</option>
                    <option value="3">Tin copy</option>
                </select>
            </td>
            <td>
                <select class="news_size size_1 form_control">
                    <option value="0">--Chọn--</option>
                    <option value="1">Tin ngắn</option>
                    <option value="2">Tin dài</option>
                    <option value="3">Bài dịch</option>
                </select>
                <select class="news_size size_2  form_control hidden">
                    <option value="0">--Chọn--</option>
                    <option value="1">Tin ngắn</option>
                    <option value="2">Tin dài</option>
                    <option value="3">Bài viết</option>
                    <option value="4">Phóng sự</option>
                </select>
                <select class="news_size size_3 form_control hidden">
                    <option value="0">--Chọn--</option>
                    <option value="1">Copy thường</option>
                </select>
            </td>
            <td>
                <select class="news_rate form_control">
                    <option value="0">--Chọn--</option>
                    <option value="1">Loại A</option>
                    <option value="2">Loại B</option>
                    <option value="3">Loại C</option>
                </select>
            </td>
            <td style="width: 100px;">
                <button type="button" class="news_solve">Tính</button>
                <span class="news_cost"></span>
                <span class="news_alert"></span>
            </td>
        </tr>    
    <?}?>
    </table>
    <div class="footer_listing" style="padding: 5px;">
        <span> * Tổng số bài viết : <?=$total->total?></span>
        &nbsp;|&nbsp;
        <span><button type="button" class="news_total">Cộng tổng : </button>&nbsp;<span id="total_cost"></span></span>
    </div>
<script>
$('.news_kind').change(function(){
    var tr = $(this).closest('tr');
    var kind = $(this).val();
    tr.find('.news_size').addClass('hidden');
    tr.find('.size_'+kind).removeClass('hidden');
    tr.find('.news_solve').trigger('click');
});
$('.news_size').change(function(){
    var tr = $(this).closest('tr');
    tr.find('.news_solve').trigger('click');
})

$('.news_rate').change(function(){
    var tr = $(this).closest('tr');
    tr.find('.news_solve').trigger('click');
});
$('.news_solve').click(function(){
    var tr = $(this).closest('tr');
    kind = tr.find('.news_kind').val();
    size = tr.find('.size_'+kind).val();
    rate = tr.find('.news_rate').val();
    
    $.ajax({
        type:'post',
        url:'ajax.php',
        data:{action:'news_cost',kind:kind,size:size,rate:rate},
        dataType:'json',
        success:function(result){
            if(!result.error){
                tr.find('.news_cost').html(result.cost);
                tr.find('.news_alert').html('');
            }else{
                tr.find('.news_cost').html(0);
                tr.find('.news_alert').html(result.error);
            }
        },
        beforeSend:function(){
            tr.find('.news_cost').html('Đang tính...');
        }
    })
});
$('.news_total').click(function(){
    var sum = 0;
    var value = 0;
    $('.news_cost').each(function(){
        if($(this).html()){
            value = parseInt($(this).html());     
            sum += value;    
        }
    })
    $('#total_cost').html(sum);
})
</script>
<?
}//End thống kê news
?>